//
//  WLLStatusOriginalView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class WLLStatusOriginalView: UIView {
        /// 记录原创微博的底部视图约束
    var OriginalViewBottomConstraint : Constraint?
 
    //MARK: - 定义一个 ViewModel 供外界赋值
    var statusViewModel : WLLStatusViewModel?{
        didSet{
            headImageView.ll_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.text = statusViewModel?.sinaSourceStr
            contentLabel.text = statusViewModel?.statusModel?.text
 
            memberShipImageView.image = statusViewModel?.mbrankImage
            statusViewModel?.statusModel?.user?.verified
            avatarImageView.image = statusViewModel?.verifiedImage
            
            //卸载约束
            OriginalViewBottomConstraint?.uninstall()
            //判断是否有配图
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {//有配图
                //赋值
                pictureView.pictureInfo = statusViewModel?.statusModel?.pic_urls
                //设置约束
              self.snp_makeConstraints(closure: { (make) in
                 OriginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
              })
                //显示配图
                pictureView.hidden = false
                
            }else{//没有视图
                //更新约束
                self.snp_makeConstraints(closure: { (make) in
                       OriginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏配图
                pictureView.hidden = true
            }
            
            
            
        }
    }
/**代码已封装**/
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    func setupUI ()  {
        //设置颜色
        //        backgroundColor = RandomColor()
        backgroundColor = OriginalColor
        //MARK: - 添加控件
          /// 01 头像
        addSubview(headImageView)
        ///02 名字
        addSubview(nameLabel)
         ///3用户等级
        addSubview(memberShipImageView)
         ///4时间
        addSubview(timeLabel)
        ///5来源
        addSubview(sourceLabel)
        ///6认证
        addSubview(avatarImageView)
          ///7微博正文
        addSubview(contentLabel)
        ///8配图
        addSubview(pictureView)
        //MARK: - 添加约束
        /// 01 头像
        headImageView.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        ///02 名字
        nameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
            make.top.equalTo(headImageView)
        }
        ///3用户等级
        memberShipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
        }
        ///4时间
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        ///5来源
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        ///6认证
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        ///7微博正文
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }
        ///8配图
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((KSCREENW-250)/2)
//            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) in
           OriginalViewBottomConstraint =  make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
    }
    
    //MARK: - 懒加载控件
    /// 头像
    private lazy var  headImageView :UIImageView = UIImageView(imageName: "avatar_default")
    /// 名字
    private lazy var nameLabel : UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        return lab
    }()
    /// 用户等级
    private lazy var memberShipImageView :UIImageView = UIImageView(imageName: "common_icon_membership")
    /// 时间
    private lazy var timeLabel : UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    /// 来源
    private lazy var sourceLabel : UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    /// 认证
    private lazy var avatarImageView : UIImageView = UIImageView(imageName: "avatar_vgirl")
    ///微博正文
    private lazy var contentLabel : UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: KSCREENW-20)
    /// 配图
    private lazy var pictureView:WLLStatusPictureView = WLLStatusPictureView()
    
}















